package org.sao.security.app.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

@Configuration
@ConditionalOnProperty(prefix="zy.security.oauth2",name="storeType",havingValue="redis")
public class TokenStoreConfig {
	
	@Autowired
	private RedisConnectionFactory  redisConnectionFactory ;

	@Bean
	public TokenStore redisTokenStore() {
		return new RedisTokenStore(redisConnectionFactory);
	}
	
	@Configuration
	@ConditionalOnProperty(prefix="zy.security.oauth2",name="storeType",havingValue="jwt",matchIfMissing=true)
	public static class JwtTokenConfig{
		
		@Bean
		public TokenStore jwtTokenStore() {
			return new JwtTokenStore(jwtAccessTokenConverter());
		}
		
		@Bean
		public JwtAccessTokenConverter jwtAccessTokenConverter() {
			JwtAccessTokenConverter jwtAccessTokenConverter =new JwtAccessTokenConverter();
			jwtAccessTokenConverter.setSigningKey("zy");
			return jwtAccessTokenConverter;
		}
		
	}
	
}
